home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / xep / imp.c < prev    next >
C/C++ Source or Header  |  1997-07-22  |  15KB  |  684 lines

  1.  
  2. static char rcsid[] =
  3.     "$Id: imp.c,v 1.2 1997/07/09 13:56:52 pvmsrc Exp $";
  4.  
  5. /*
  6.  *         PVM version 3.4:  Parallel Virtual Machine System
  7.  *               University of Tennessee, Knoxville TN.
  8.  *           Oak Ridge National Laboratory, Oak Ridge TN.
  9.  *                   Emory University, Atlanta GA.
  10.  *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
  11.  *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
  12.  *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
  13.  *                   (C) 1997 All Rights Reserved
  14.  *
  15.  *                              NOTICE
  16.  *
  17.  * Permission to use, copy, modify, and distribute this software and
  18.  * its documentation for any purpose and without fee is hereby granted
  19.  * provided that the above copyright notice appear in all copies and
  20.  * that both the copyright notice and this permission notice appear in
  21.  * supporting documentation.
  22.  *
  23.  * Neither the Institutions (Emory University, Oak Ridge National
  24.  * Laboratory, and University of Tennessee) nor the Authors make any
  25.  * representations about the suitability of this software for any
  26.  * purpose.  This software is provided ``as is'' without express or
  27.  * implied warranty.
  28.  *
  29.  * PVM version 3 was funded in part by the U.S. Department of Energy,
  30.  * the National Science Foundation and the State of Tennessee.
  31.  */
  32.  
  33. /*
  34.  *    imp.c
  35.  *
  36.  *    X device-independent image display
  37.  *
  38.  *    Oct 95 Manchek
  39.  */
  40.  
  41.  
  42. #ifdef HASSTDLIB
  43. #include <stdlib.h>
  44. #endif
  45. #include <stdio.h>
  46. #include <math.h>
  47. #include <X11/Xlib.h>
  48. #include <X11/cursorfont.h>
  49. #include <X11/Intrinsic.h>
  50. #include <X11/StringDefs.h>
  51. #include <X11/Shell.h>
  52. #include <X11/Xaw/Label.h>
  53. #include <X11/Xaw/Command.h>
  54. #include <X11/Xaw/Toggle.h>
  55. #include <X11/Xaw/Form.h>
  56. #include "../src/bfunc.h"
  57. #include "myalloc.h"
  58. #include "imp.h"
  59.  
  60. #define    MTALLOC(n,t,g)        (t*)malloc((n)*sizeof(t))
  61.  
  62. #ifndef    min
  63. #define    min(a,b)    ((a)<(b)?(a):(b))
  64. #endif
  65. #ifndef    max
  66. #define    max(a,b)    ((a)>(b)?(a):(b))
  67. #endif
  68.  
  69. #ifndef    NCMV
  70. #define    NCMV    128
  71. #endif
  72.  
  73. #define    byteswide(w) (((w)+7)/8)
  74.  
  75.  
  76. /***************
  77.  **  Globals  **
  78.  **           **
  79.  ***************/
  80.  
  81. extern Display *xDisp;            /* from xep.c */
  82.  
  83. static Colormap myCmap = 0;
  84. static Visual *myVisual = 0;
  85. static GC myGC = 0;                /* canvas painting gc */
  86. static unsigned long *cmapColors = 0;
  87. static unsigned int *lutr = 0;    /* lut for non-colormapped displays */
  88. static unsigned int *lutg = 0;
  89. static unsigned int *lutb = 0;
  90.  
  91. static int ncmv = 0;
  92. static int isMono = 0;
  93. static int nPlanes = 0;            /* display depth */
  94. static int revByte = 0;            /* X server byte order is opposite ours */
  95. static int bimbo = 0;            /* bitmap bit order */
  96. static int xBpp = 0;            /* ximage bits per pixel */
  97. static int xBypp = 0;            /* ximage bytes per pixel */
  98. static int redMask = 0;
  99. static int redShift = 0;
  100. static int greenMask = 0;
  101. static int greenShift = 0;
  102. static int blueMask = 0;
  103. static int blueShift = 0;
  104.  
  105.  
  106. /***************
  107.  **  Xt Gorp  **
  108.  **           **
  109.  ***************/
  110.  
  111. /*
  112. *    Ordered dither for worst case / binary displays
  113. */
  114.  
  115. u_char ditclass[8][8] = {
  116.     2,   130, 34,  162, 10,  138, 42,  170,
  117.     194, 66,  226, 98,  202, 74,  234, 106,
  118.     50,  178, 18,  146, 58,  186, 26,  154,
  119.     242, 114, 210, 82,  250, 122, 218, 90,
  120.     14,  142, 46,  174, 6,   134, 38,  166,
  121.     206, 78,  238, 110, 198, 70,  230, 102,
  122.     62,  190, 30,  158, 54,  182, 22,  150,
  123.     254, 126, 222, 94,  246, 118, 214, 86
  124. };
  125.  
  126.  
  127. setup_color(cnp, forcemono, falsecolor)
  128.     struct canvas *cnp;
  129.     int forcemono;
  130.     int falsecolor;
  131. {
  132.     int i, j;
  133.     XColor colr;
  134.     unsigned int rbor[NCMV], rbog[NCMV], rbob[NCMV];
  135.     unsigned int mbo = 0x04030201;    /* to test machine byte order */
  136.     int xScrn = DefaultScreen(xDisp);
  137.     XGCValues xgcv;
  138.     XWindowAttributes wat;
  139.  
  140.     XGetWindowAttributes(xDisp, cnp->cn_win, &wat);
  141.  
  142.     nPlanes = wat.depth;
  143.     myVisual = wat.visual;
  144.     myCmap = wat.colormap;
  145.  
  146.     if (cmapColors && ncmv > 0) {
  147.         XFreeColors(xDisp, myCmap, cmapColors, ncmv, (long)0);
  148.         MY_FREE(cmapColors);
  149.     }
  150.     cmapColors = 0;
  151.     ncmv = 0;
  152.     if (lutr)
  153.         MY_FREE(lutr);
  154.     if (lutg)
  155.         MY_FREE(lutg);
  156.     if (lutb)
  157.         MY_FREE(lutb);
  158.     lutr = lutg = lutb = 0;
  159.  
  160.     if (myGC)
  161.         XFreeGC(xDisp, myGC);
  162.     xgcv.function = GXcopy;
  163.     xgcv.background = BlackPixel(xDisp, xScrn);
  164.     xgcv.foreground = WhitePixel(xDisp, xScrn);
  165.     myGC = XCreateGC(xDisp, cnp->cn_win,
  166.             GCBackground|GCForeground|GCFunction, &xgcv);
  167.  
  168.     revByte = (ImageByteOrder(xDisp) == MSBFirst) ? 1 : 0;
  169.     if (*(char *)&mbo == 4)
  170.         revByte = !revByte;
  171.  
  172.     bimbo = BitmapBitOrder(xDisp);
  173.  
  174.     if (!forcemono && nPlanes > 1) {
  175.         isMono = 0;
  176.  
  177.         if (myVisual->class == TrueColor || myVisual->class == DirectColor) {
  178.             XPixmapFormatValues *pfv;
  179.             int i;
  180.  
  181.             if (!(pfv = XListPixmapFormats(xDisp, &i))) {
  182.                 fprintf(stderr, "can't get pixmap format list for screen\n");
  183.                 exit(1);
  184.             }
  185.             while (--i >= 0)
  186.                 if (pfv[i].depth == nPlanes) break;
  187.             if (i < 0) {
  188.                 fprintf(stderr, "no pixmap format matches screen depth?\n");
  189.                 exit(1);
  190.             }
  191.             xBpp = pfv[i].bits_per_pixel;
  192.             xBypp = xBpp / 8;
  193.             redMask = myVisual->red_mask;
  194.             redShift = ffs(redMask & ~(redMask >> 1)) - 8;
  195.             greenMask = myVisual->green_mask;
  196.             greenShift = ffs(greenMask & ~(greenMask >> 1)) - 8;
  197.             blueMask = myVisual->blue_mask;
  198.             blueShift = ffs(blueMask & ~(blueMask >> 1)) - 8;
  199. #ifdef    DEBUG
  200.             fprintf(stderr,
  201.                 "np %d  r 0x%x %d  g 0x%x %d  b 0x%x %d  bpp %d bypp %d\n",
  202.                 nPlanes,
  203.                 redMask, redShift, greenMask, greenShift, blueMask, blueShift,
  204.                 xBpp, xBypp);
  205. #endif
  206.             lutr = TALLOC(256, unsigned int, "lut");
  207.             lutg = TALLOC(256, unsigned int, "lut");
  208.             lutb = TALLOC(256, unsigned int, "lut");
  209.             if (falsecolor)
  210.                 mkrbow(lutr, lutg, lutb, 256, 256);
  211.             else
  212.                 for (i = 0; i < 256; i++)
  213.                     lutr[i] = lutg[i] = lutb[i] = i;
  214.             return 0;
  215.  
  216.         } else {
  217.             cmapColors = TALLOC(NCMV, unsigned long, "cmap");
  218.             ncmv = NCMV;
  219.             while (ncmv > 3) {
  220. #ifdef    DEBUG
  221.                 fprintf(stderr, "setup_color() trying %d cells\n", ncmv);
  222. #endif
  223.                 j = 17 - ffs(ncmv);
  224.                 if (falsecolor)
  225.                     mkrbow(rbor, rbog, rbob, 65536, ncmv);
  226.                 for (i = 0; i < ncmv; i++) {
  227.                     if (falsecolor) {
  228.                         colr.red = rbor[i];
  229.                         colr.green = rbog[i];
  230.                         colr.blue = rbob[i];
  231.                     } else
  232.                         colr.red = colr.green = colr.blue = (i * 65536) / ncmv;
  233.                     if (!XAllocColor(xDisp, myCmap, &colr)) {
  234.                         if (i > 0) {
  235. #ifdef    DEBUG
  236.                             fprintf(stderr, "recycling %d color cells\n", i);
  237. #endif
  238.                             XFreeColors(xDisp, myCmap, cmapColors, i, (long)0);
  239.                         }
  240.                         break;
  241.                     }
  242.                     cmapColors[i] = colr.pixel;
  243.                 }
  244.                 if (i == ncmv)
  245.                     return 0;
  246. /*
  247.                 ncmv = i;
  248. */
  249.                 ncmv /= 2;
  250.             }
  251.             fputs("couldn't alloc enough color cells\n", stderr);
  252.             MY_FREE(cmapColors);
  253.             cmapColors = 0;
  254.         }
  255.     }
  256.  
  257.     isMono = 1;
  258.     fputs("display is binary, it's not gonna look great...\n", stderr);
  259.     return 0;
  260. }
  261.  
  262.  
  263. /*    mkrbow()
  264. *
  265. *    Generate a rainbow lookup table.  0 is black, len-1 is white, and entries
  266. *    between those two go through the spectrum from red to violet.
  267. *    Output values are from 0 to mxval-1.
  268. */
  269.  
  270. static int
  271. mkrbow(r, g, b, mxval, len)
  272.     unsigned int *r, *g, *b;    /* red, grn, blu lut return */
  273.     int mxval;                    /* max value */
  274.     int len;                    /* number of entries (length of r, g, b) */
  275. {
  276.     int i, j;
  277.     double d, e;
  278.  
  279.     for (i = 1; i < len - 1; i++) {
  280.         j = len - 1 - i;
  281.         d = (d = cos((double)((j - len * 0.16) * (3.1415926535 / len)))) < 0.0
  282.             ? 0.0 : d;
  283.         b[i] = d * mxval;
  284.         d = (d = cos((double)((j - len * 0.52) * (3.1415926535 / len)))) < 0.0
  285.             ? 0.0 : d;
  286.         g[i] = d * mxval;
  287.         d = (d = cos((double)((j - len * .83) * (3.1415926535 / len)))) < 0.0
  288.             ? 0.0 : d;
  289.         e = (e = cos((double)(j * (3.1415926535 / len)))) < 0.0
  290.             ? 0.0 : e;
  291.         r[i] = d * mxval + e * (mxval / 2);
  292.     }
  293.     r[i] = g[i] = b[i] = mxval - 1;
  294.     r[0] = g[0] = b[0] = 0;
  295.     return 0;
  296. }
  297.  
  298.  
  299. /*    bsw()
  300. *
  301. *    Byteswap an integer.
  302. */
  303.  
  304. bsw(p, n)
  305.     char *p;
  306.     int n;
  307. {
  308.     char c;
  309.     char *q;
  310.  
  311.     switch (n) {
  312.     case 4:
  313.         c = p[0];
  314.         p[0] = p[3];
  315.         p[3] = c;
  316.         c = p[1];
  317.         p[1] = p[2];
  318.         p[2] = c;
  319.         break;
  320.  
  321.     case 8:
  322.         c = p[0];
  323.         p[0] = p[7];
  324.         p[7] = c;
  325.         c = p[1];
  326.         p[1] = p[6];
  327.         p[6] = c;
  328.         c = p[2];
  329.         p[2] = p[5];
  330.         p[5] = c;
  331.         c = p[3];
  332.         p[3] = p[4];
  333.         p[4] = c;
  334.         break;
  335.  
  336.     case 2:
  337.         c = p[0];
  338.         p[0] = p[1];
  339.         p[1] = c;
  340.         break;
  341.  
  342.     default:
  343.         q = p + n - 1;
  344.         while (p < q) {
  345.             c = *p;
  346.             *p++ = *q;
  347.             *q-- = c;
  348.         }
  349.         break;
  350.     }
  351.     return 0;
  352. }
  353.  
  354.  
  355. /*    repaint_region()
  356. *
  357. *    Replot XImage from its underlying image.
  358. */
  359.  
  360. repaint_region(cnp, x1, y1, x2, y2)
  361.     struct canvas *cnp;
  362.     int x1, y1, x2, y2;                /* image coordinates */
  363. {
  364.     char *ximbuf;                    /* ximage data */
  365.     u_char *ribuf;                    /* src image data */
  366.  
  367.     if (x1 < cnp->cn_ox)
  368.         x1 = cnp->cn_ox;
  369.     if (y1 < cnp->cn_oy)
  370.         y1 = cnp->cn_oy;
  371.     if (++x2 > cnp->cn_wd)
  372.         x2 = cnp->cn_wd;
  373.     if (++y2 > cnp->cn_ht)
  374.         y2 = cnp->cn_ht;
  375.  
  376.     ximbuf = cnp->cn_xim->data;
  377.     ribuf = cnp->cn_dat;
  378.  
  379.     if (isMono) {
  380.  
  381.     /*
  382.     * binary display - XXX region not done yet
  383.     */
  384.  
  385.         int sx, sy;                        /* source x, y counter */
  386.         int dx, dy;                        /* dest x, y counter */
  387.         int xa, ya;
  388.         unsigned char *sa;
  389.         char *da;
  390.         u_char pxa;
  391.         u_char msks[8];
  392.         int lbs;
  393.  
  394.         for (lbs = 0; lbs < 8; lbs++)
  395.             if (bimbo == LSBFirst)
  396.                 msks[lbs] = 1 << lbs;
  397.             else
  398.                 msks[7 - lbs] = 1 << lbs;
  399.         sy = cnp->cn_oy;
  400.         dy = 0;
  401.         ya = 0;
  402.         while (sy < cnp->cn_ht && dy < cnp->cn_ht) {
  403.             sx = cnp->cn_ox;
  404.             dx = 0;
  405.             xa = 0;
  406.             sa = ribuf + sy * cnp->cn_wd + sx;
  407.             da = ximbuf + dy * cnp->cn_xim->bytes_per_line;
  408.             pxa = 0;
  409.             while (sx < cnp->cn_wd && dx < cnp->cn_wd) {
  410.                 if (*sa > ditclass[dx & 7][dy & 7])
  411.                     pxa |= msks[dx & 7];
  412.                 if (!(++dx & 7)) {
  413.                     *da++ = pxa;
  414.                     pxa = 0;
  415.                 }
  416.                 if (++xa >= cnp->cn_zoom) {
  417.                     xa = 0;
  418.                     sx++;
  419.                     sa++;
  420.                 }
  421.             }
  422.             if (dx & 7)
  423.                 *da++ = pxa;
  424.             dy++;
  425.             if (++ya >= cnp->cn_zoom) {
  426.                 ya = 0;
  427.                 sy++;
  428.             }
  429.         }
  430.  
  431.     } else if (cmapColors) {
  432.  
  433.     /*
  434.     * colormapped display
  435.     */
  436.  
  437.         int sx, sy;                        /* source x, y counter */
  438.         int dx, dy;                        /* dest x, y counter */
  439.         int xa, ya;
  440.         unsigned char *sa;
  441.         char *da;
  442.  
  443.         sy = y1;
  444.         dy = (y1 - cnp->cn_oy) * cnp->cn_zoom;
  445.         ya = 0;
  446.         while (sy < y2 && dy < cnp->cn_ht) {
  447.             sx = x1;
  448.             dx = (x1 - cnp->cn_ox) * cnp->cn_zoom;
  449.             xa = 0;
  450.             sa = ribuf + sy * cnp->cn_wd + sx;
  451.             da = ximbuf + dy * cnp->cn_xim->bytes_per_line + dx;
  452.             while (sx < x2 && dx < cnp->cn_wd) {
  453.                 *da = cmapColors[(*sa * ncmv) / 256];
  454.                 da++;
  455.                 dx++;
  456.                 if (++xa >= cnp->cn_zoom) {
  457.                     xa = 0;
  458.                     sx++;
  459.                     sa++;
  460.                 }
  461.             }
  462.             dy++;
  463.             if (++ya >= cnp->cn_zoom) {
  464.                 ya = 0;
  465.                 sy++;
  466.             }
  467.         }
  468.  
  469.     } else {
  470.  
  471.     /*
  472.     * non-colormapped display
  473.     */
  474.         int sx, sy;                        /* source x, y counter */
  475.         int dx, dy;                        /* dest x, y counter */
  476.         int xa, ya;
  477.         unsigned char *sa;
  478.         char *da;
  479.  
  480.         if (xBypp == sizeof(unsigned int)) {
  481.             unsigned int pixv;
  482.             unsigned int ps, pixr, pixg, pixb;
  483.  
  484.             sy = y1;
  485.             dy = (y1 - cnp->cn_oy) * cnp->cn_zoom;
  486.             ya = 0;
  487.             while (sy < y2 && dy < cnp->cn_ht) {
  488.                 sx = x1;
  489.                 dx = (x1 - cnp->cn_ox) * cnp->cn_zoom;
  490.                 xa = 0;
  491.                 sa = ribuf + sy * cnp->cn_wd + sx;
  492.                 da = ximbuf + dy * cnp->cn_xim->bytes_per_line + dx * xBypp;
  493.                 while (sx < x2 && dx < cnp->cn_wd) {
  494.                     ps = 0xff & *sa;
  495.                     pixr = lutr[ps];
  496.                     pixg = lutg[ps];
  497.                     pixb = lutb[ps];
  498.                     if (redShift >= 0)
  499.                         pixv = redMask & (pixr << redShift);
  500.                     else
  501.                         pixv = redMask & (pixr >> -redShift);
  502.                     if (greenShift >= 0)
  503.                         pixv |= greenMask & (pixg << greenShift);
  504.                     else
  505.                         pixv |= greenMask & (pixg >> -greenShift);
  506.                     if (blueShift >= 0)
  507.                         pixv |= blueMask & (pixb << blueShift);
  508.                     else
  509.                         pixv |= blueMask & (pixb >> -blueShift);
  510.                     if (revByte)
  511.                         bsw((char *)&pixv, sizeof(pixv));
  512.                     *((unsigned int *)da) = pixv;
  513.         
  514.                     da += xBypp;
  515.                     dx++;
  516.                     if (++xa >= cnp->cn_zoom) {
  517.                         xa = 0;
  518.                         sx++;
  519.                         sa++;
  520.                     }
  521.                 }
  522.                 dy++;
  523.                 if (++ya >= cnp->cn_zoom) {
  524.                     ya = 0;
  525.                     sy++;
  526.                 }
  527.             }
  528.  
  529.         } else if (xBypp == sizeof(unsigned short)) {
  530.             unsigned short pixv;
  531.             unsigned int ps, pixr, pixg, pixb;
  532.  
  533.             sy = y1;
  534.             dy = (y1 - cnp->cn_oy) * cnp->cn_zoom;
  535.             ya = 0;
  536.             while (sy < y2 && dy < cnp->cn_ht) {
  537.                 sx = x1;
  538.                 dx = (x1 - cnp->cn_ox) * cnp->cn_zoom;
  539.                 xa = 0;
  540.                 sa = ribuf + sy * cnp->cn_wd + sx;
  541.                 da = ximbuf + dy * cnp->cn_xim->bytes_per_line + dx * xBypp;
  542.                 while (sx < x2 && dx < cnp->cn_wd) {
  543.                     ps = 0xff & *sa;
  544.                     pixr = lutr[ps];
  545.                     pixg = lutg[ps];
  546.                     pixb = lutb[ps];
  547.                     if (redShift >= 0)
  548.                         pixv = redMask & (pixr << redShift);
  549.                     else
  550.                         pixv = redMask & (pixr >> -redShift);
  551.                     if (greenShift >= 0)
  552.                         pixv |= greenMask & (pixg << greenShift);
  553.                     else
  554.                         pixv |= greenMask & (pixg >> -greenShift);
  555.                     if (blueShift >= 0)
  556.                         pixv |= blueMask & (pixb << blueShift);
  557.                     else
  558.                         pixv |= blueMask & (pixb >> -blueShift);
  559.                     if (revByte)
  560.                         bsw((char *)&pixv, sizeof(pixv));
  561.                     *((unsigned short *)da) = pixv;
  562.         
  563.                     da += xBypp;
  564.                     dx++;
  565.                     if (++xa >= cnp->cn_zoom) {
  566.                         xa = 0;
  567.                         sx++;
  568.                         sa++;
  569.                     }
  570.                 }
  571.                 dy++;
  572.                 if (++ya >= cnp->cn_zoom) {
  573.                     ya = 0;
  574.                     sy++;
  575.                 }
  576.             }
  577.  
  578.         } else if (xBypp == sizeof(unsigned char)) {
  579.             unsigned char pixv;
  580.             unsigned int ps, pixr, pixg, pixb;
  581.  
  582.             sy = y1;
  583.             dy = (y1 - cnp->cn_oy) * cnp->cn_zoom;
  584.             ya = 0;
  585.             while (sy < y2 && dy < cnp->cn_ht) {
  586.                 sx = x1;
  587.                 dx = (x1 - cnp->cn_ox) * cnp->cn_zoom;
  588.                 xa = 0;
  589.                 sa = ribuf + sy * cnp->cn_wd + sx;
  590.                 da = ximbuf + dy * cnp->cn_xim->bytes_per_line + dx * xBypp;
  591.                 while (sx < x2 && dx < cnp->cn_wd) {
  592.                     ps = 0xff & *sa;
  593.                     pixr = lutr[ps];
  594.                     pixg = lutg[ps];
  595.                     pixb = lutb[ps];
  596.                     if (redShift >= 0)
  597.                         pixv = redMask & (pixr << redShift);
  598.                     else
  599.                         pixv = redMask & (pixr >> -redShift);
  600.                     if (greenShift >= 0)
  601.                         pixv |= greenMask & (pixg << greenShift);
  602.                     else
  603.                         pixv |= greenMask & (pixg >> -greenShift);
  604.                     if (blueShift >= 0)
  605.                         pixv |= blueMask & (pixb << blueShift);
  606.                     else
  607.                         pixv |= blueMask & (pixb >> -blueShift);
  608.                     *((unsigned char *)da) = pixv;
  609.         
  610.                     da += xBypp;
  611.                     dx++;
  612.                     if (++xa >= cnp->cn_zoom) {
  613.                         xa = 0;
  614.                         sx++;
  615.                         sa++;
  616.                     }
  617.                 }
  618.                 dy++;
  619.                 if (++ya >= cnp->cn_zoom) {
  620.                     ya = 0;
  621.                     sy++;
  622.                 }
  623.             }
  624.  
  625.         } else {
  626.             fprintf(stderr, "aaugh, i can't do %d byte true color pixels\n",
  627.                     xBypp);
  628.             exit(1);
  629.         }
  630.     }
  631.     return 0;
  632. }
  633.  
  634.  
  635. /*    cre_xim()
  636. *
  637. *    Create (or change) XImage for a canvas.
  638. */
  639.  
  640. cre_xim(cnp)
  641.     struct canvas *cnp;
  642. {
  643.     char *ximbuf;
  644.     int wd = cnp->cn_wd;
  645.     int ht = cnp->cn_ht;
  646.  
  647.     if (cnp->cn_xim)
  648.         XDestroyImage(cnp->cn_xim);
  649.  
  650.     if (isMono) {
  651.         ximbuf = MTALLOC(byteswide(wd) * ht, char, "imb");
  652.         cnp->cn_xim = XCreateImage(xDisp, myVisual, 1, XYBitmap, 0,
  653.             ximbuf, wd, ht, 8, byteswide(wd));
  654.  
  655.     } else if (cmapColors) {
  656.         ximbuf = MTALLOC(wd * ht, char, "imb");
  657.         cnp->cn_xim = XCreateImage(xDisp, myVisual, nPlanes, ZPixmap, 0,
  658.             ximbuf, wd, ht, 8, wd);
  659.  
  660.     } else {
  661.         ximbuf = MTALLOC(wd * ht * xBypp, char, "imb");
  662.         cnp->cn_xim = XCreateImage(xDisp, myVisual, nPlanes, ZPixmap, 0,
  663.             ximbuf, wd, ht, xBpp, wd * xBypp);
  664.     }
  665.     return 0;
  666. }
  667.  
  668.  
  669. /*    refresh_region()
  670. *
  671. *    Refresh a canvas window.
  672. */
  673.  
  674. refresh_region(cnp, x1, y1, x2, y2)
  675.     struct canvas *cnp;
  676.     int x1, y1, x2, y2;
  677. {
  678.     XPutImage(xDisp, cnp->cn_win, myGC, cnp->cn_xim,
  679.             x1, y1, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
  680.     return 0;
  681. }
  682.  
  683.  
  684.